﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;

namespace PJ.Framework
{
    /// <summary>
    /// Logger class
    /// </summary>
    public static class Log
    {
        /// <summary>
        /// Write to Log
        /// </summary>
        /// <param name="message">message to log</param>
        /// <param name="category">log category</param>
        /// <param name="type">log <see cref="TraceEventType">type</see></param>
        public static void Write(string message, string category, TraceEventType type)
        {
            Logger.Write(message, category, 5, 0, type);
        }
        
        /// <summary>
        /// Write information message to log.
        /// </summary>
        /// <param name="message">message to log</param>
        public static void WriteInfo(string message)
        {
            WriteInfo(message, "General");
        }
        
        /// <summary>
        /// Write information message to log.
        /// </summary>
        /// <param name="message">message to log</param>
        /// <param name="category">log category</param>
        public static void WriteInfo(string message, string category)
        {
            Write(message, category, TraceEventType.Information);
        }
        
        /// <summary>
        /// Write a warning message to log.
        /// </summary>
        /// <param name="message">message to log</param>
        public static void WriteWarning(string message)
        {
            WriteWarning(message, "General");
        }
        
        /// <summary>
        /// Write a warning message to log.
        /// </summary>
        /// <param name="message">message to log</param>
        /// <param name="category">log category</param>
        public static void WriteWarning(string message, string category)
        {
            Write(message, category, TraceEventType.Warning);
        }
        
        /// <summary>
        /// Write an Error message to log.
        /// </summary>
        /// <param name="message">message to log</param>
        public static void WriteError(string message)
        {
            WriteError(message, "General");
        }
        
        /// <summary>
        /// Write an Error message to log.
        /// </summary>
        /// <param name="message">message to log</param>
        /// <param name="category">log category</param>
        public static void WriteError(string message, string category)
        {
            Write(message, category, TraceEventType.Error);
        }

        /// <summary>
        /// Write a Start message to log.
        /// </summary>
        /// <param name="message">message to log</param>
        public static void WriteStart(string message)
        {
            WriteStart(message, "General");
        }

        /// <summary>
        /// Write a Start message to log.
        /// </summary>
        /// <param name="message">message to log</param>
        /// <param name="category">log category</param>
        public static void WriteStart(string message, string category)
        {
            Write(message, category, TraceEventType.Error);
        }

        /// <summary>
        /// Write a Stop message to log.
        /// </summary>
        /// <param name="message">message to log</param>
        public static void WriteStop(string message)
        {
            WriteStop(message, "General");
        }

        /// <summary>
        /// Write a Stop message to log.
        /// </summary>
        /// <param name="message">message to log</param>
        /// <param name="category">log category</param>
        public static void WriteStop(string message, string category)
        {
            Write(message, category, TraceEventType.Stop);
        }

        /// <summary>
        /// Write an application start information message
        /// </summary>
        public static void WriteApplicationStart()
        {
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            ManagedSecurityContextInformationProvider informationHelper = new ManagedSecurityContextInformationProvider();
            informationHelper.PopulateDictionary(dictionary);
            string message = "Application " + AppDomain.CurrentDomain.FriendlyName + " Started on computer "
                            + System.Environment.MachineName + " by user " + System.Environment.UserName + "@" + System.Environment.UserDomainName;
            Logger.Write(message, "ApplicationStart", 5, 0, TraceEventType.Start, "Application start", dictionary);
        }

        /// <summary>
        /// Write an application stop information message
        /// </summary>
        public static void WriteApplicationStop()
        {
            string message = "Application " + AppDomain.CurrentDomain.FriendlyName + " Stopped on computer "
                            + System.Environment.MachineName + " by user " + System.Environment.UserName + "@" + System.Environment.UserDomainName;
            Logger.Write(message, "ApplicationStart", 5, 0, TraceEventType.Stop, "Application Stop");
        }
    }
}
